package basic.study.leetcode;

/**
 * @ClassName Mid75
 * @Description 颜色分类
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/24 9:22
 * @Version 1.0
 */
public class Mid75 {
    /**
     * 荷兰三色国旗问题
     */
    class Solution {
        public void sortColors(int[] nums) {
            if (nums == null || nums.length == 0) {
                return;
            }
            int len = nums.length;
            int p0 = 0;
            int p1 = 0;
            int p2 = len - 1;
            while (p1 <= p2) {
                if (nums[p1] == 1) {
                    p1++;
                } else if (nums[p1] == 0) {
                    swap(nums, p0, p1);
                    p0++;
                    p1++;
                } else {//==2
                    swap(nums, p1, p2);
                    p2--;
                }
            }
        }

        public void swap(int[] nums, int p0, int p1) {
            int temp = nums[p0];
            nums[p0] = nums[p1];
            nums[p1] = temp;
        }
    }
}
